// pages/my-pages/top-up-list/index.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    actionText: '查询',
    topUpList: [],
    total: 0, //分页总数
    page: 1, //分页记录数
    pageSize: 10, //分页大小
    onRefresh: true,
    showTextAndTitleWithInput: false,
    item: {}
  },
  //点击了添加按钮
  addHandleClick: function (e) {
    wx.navigateTo({
      url: '/pages/my-pages/top-up-add/index',
    })
  },
  //点击删除弹窗的确定
  confirmChange: function (e) {
    let that = this;
    let item = that.data.item;
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'topUpBill/delTopUp?id=' + item.id
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        that.setData({
          showTextAndTitleWithInput: false,
          item: {},
          onRefresh: false,
          page: 1,
          topUpList: []
        });
        that.getTopUpList();
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  //点击删除弹窗的取消
  closeDialog: function () {
    this.setData({
      ['showTextAndTitleWithInput']: false
    });
  },
  //点击了删除按钮
  onDelete: function (e) {
    let item = e.currentTarget.dataset.entity;
    this.setData({
      ['showTextAndTitleWithInput']: true,
      item: item
    })
  },
  //点击了查询按钮
  actionHandle: function () {
    this.setData({
      onRefresh: false,
      page: 1
    });
    this.getTopUpList();
  },
  //输入框内容改变函数
  changeHandle: function (e) {
    this.setData({
      value: e.detail.value
    })
  },
  //输入框点击了清除按钮
  clearHandle: function (e) {
    this.setData({
      value: ''
    });
  },
  //打开充值详情页
  toDetail: function (e) {
    let item = e.currentTarget.dataset.entity;
    wx.navigateTo({
      url: '/pages/my-pages/top-up-detail/index?id=' + item.id,
    })
  },
  //查询充值记录
  getTopUpList: function () {
    let that = this;
    var onRefresh = that.data.onRefresh; //false为重新刷新数据，true为分页累加数据
    let obj = {
      method: 'POST',
      showLoading: false,
      url: 'topUpBill/getPage',
      data: {
        'nameOrNo': that.data.value,
        'page': that.data.page,
        'size': that.data.pageSize,
        'column': 'billTime',
        'order': 'desc'
      }
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        var datas = res.data.data.records;
        if (datas != null && datas.length > 0) {
          if (onRefresh) { //false为重新刷新数据，true为分页累加数据
            that.setData({
              topUpList: that.data.topUpList.concat(datas),
              page: res.data.data.current,
              total: res.data.data.pages
            })
          } else {
            that.setData({
              topUpList: datas,
              page: res.data.data.current,
              total: res.data.data.pages
            })
          }
        } else {
          if (!onRefresh) {
            that.setData({
              topUpList: []
            })
          }
          ui.showToast('没有更多数据了')
        }
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      onRefresh: false,
      page: 1,
      topUpList: [],
    });
    this.getTopUpList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      onRefresh: false,
      page: 1,
      topUpList: [],
    });
    this.getTopUpList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let that = this;
    this.setData({
      onRefresh: true,
      page: that.data.page + 1
    });
    this.getTopUpList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})